package array.leetcode.easy;

/**
 * @author bruin_du
 * @description 求平方根
 * @date 2022/8/29 23:48
 **/
public class OfferII072_MySqrt {
    public int mySqrt(int x) {
        if (x == 1) return 1;
        int left = 0, right = x, ans = 0;
        while (left <= right) {
            int mid = left + (right - left >> 1);
            if ((long) mid * mid <= x) {
                ans = mid;
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return ans;
    }
}
